﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;
using DACNPM.MyUserControl.UCHienThi;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCSuaHang : UserControl
    {
        HangHoaBUO haBUO = new HangHoaBUO();
        NhaCungCapBUO nccBUO = new NhaCungCapBUO();
        NumberBUO numBUO = new NumberBUO();

        String idHa;
        int id;

        public UCSuaHang(String id)
        {
            InitializeComponent();
            showCombobox();
            //binding();
            lblIDHang.Text = id.ToString();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = haBUO.HienThiHangHoa();
            tbl_Hang ha = haBUO.GetSingle(id.Trim());
            txtTenHang.Text = ha.TenHang;
            txtDonGia.Text = ha.DonGia.ToString();
            dateTimePicker1.Text = ha.NSX.ToString();
        }

        void showCombobox()
        {
            cbxNCC.DataSource = nccBUO.GetAll();
            cbxNCC.DisplayMember = "TenNhaCungCap";
            cbxNCC.ValueMember = "id_NhaCungCap";
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            txtDonGia.Text = "";
            txtTenHang.Text = "";
            cbxNCC.Text = "";
        }

        private void rdXemTatCa_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoTen.Text = "";
            txtTimTheoTen.Enabled = false;
            txtTimTheoMaHang.Text = "";
            txtTimTheoMaHang.Enabled = false;
            dateTimePicker2.Enabled = false;
            dataGridView1.DataSource = haBUO.HienThiHangHoa();
        }

        private void rdTimTheoTenHang_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoTen.Text = "";
            txtTimTheoTen.Enabled = true;
            txtTimTheoMaHang.Text = "";
            txtTimTheoMaHang.Enabled = false;
            dateTimePicker2.Enabled = false;
            dataGridView1.DataSource = haBUO.HienThiHangHoa();
            dataGridView1.Enabled = true;
            binding();
        }

        private void rdTimTheoMaHang_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoTen.Text = "";
            txtTimTheoTen.Enabled = false;
            txtTimTheoMaHang.Text = "";
            txtTimTheoMaHang.Enabled = true;
            dateTimePicker2.Enabled = false;
            dataGridView1.DataSource = haBUO.HienThiHangHoa();
            dataGridView1.Enabled = true;
            binding();
        }

        private void rdNgaySX_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoTen.Text = "";
            txtTimTheoTen.Enabled = false;
            txtTimTheoMaHang.Text = "";
            txtTimTheoMaHang.Enabled = false;
            dateTimePicker2.Enabled = true;
            dataGridView1.DataSource = haBUO.HienThiHangHoa();
            dataGridView1.Enabled = true;
            binding();
        }

        private void txtTimTheoTen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rdTimTheoTenHang.Checked == true)
                {
                    if (txtTimTheoTen.Text.Trim().Equals(""))
                    {
                        MessageBox.Show("Chưa nhập tên hàng hóa cần tìm");
                    }
                    else
                    {
                        List<tbl_Hang> list = haBUO.searchByTenHang(txtTimTheoTen.Text.Trim());

                        if (list.Count > 0)
                        {
                            dataGridView1.AutoGenerateColumns = false;
                            dataGridView1.DataSource = list;
                            dataGridView1.Visible = true;
                        }
                        else
                        {
                            dataGridView1.Visible = true;
                            MessageBox.Show("Không có hàng hóa cần tìm");
                        }
                    }
                }
            }
        }

        private void txtTimTheoMaNV_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rdTimTheoMaHang.Checked == true)
                {
                    if (txtTimTheoMaHang.Text.Trim().Equals(""))
                    {
                        MessageBox.Show("Chưa nhập mã hàng hóa cần tìm");
                    }
                    else
                    {
                        List<tbl_Hang> list = haBUO.searchByMaHang(txtTimTheoMaHang.Text.Trim());

                        if (list.Count > 0)
                        {
                            dataGridView1.AutoGenerateColumns = false;
                            dataGridView1.DataSource = list;
                            dataGridView1.Visible = true;
                        }
                        else
                        {
                            dataGridView1.Visible = true;
                            MessageBox.Show("Không có hàng hóa cần tìm");
                        }
                    }
                }
            }
        }

        private void txtTimTheoTen_TextChanged(object sender, EventArgs e)
        {
            List<tbl_Hang> list = haBUO.searchByTenHang(txtTimTheoTen.Text.Trim());

            if (list.Count > 0)
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = list;
                dataGridView1.Visible = true;
            }
        }

        private void txtTimTheoMaHang_TextChanged(object sender, EventArgs e)
        {
            List<tbl_Hang> list = haBUO.searchByMaHang(txtTimTheoMaHang.Text.Trim());

            if (list.Count > 0)
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = list;
                dataGridView1.Visible = true;
            }
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            List<tbl_Hang> list = haBUO.searchByNgaySX(DateTime.Parse(dateTimePicker1.Text));

            if (list.Count > 0)
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = list;
                dataGridView1.Visible = true;
            }
        }

        /// <summary>
        /// binding du lieu vao textbox
        /// </summary>
        public void binding()
        {
            lblIDHang.DataBindings.Clear();
            lblIDHang.DataBindings.Add("Text", dataGridView1.DataSource, "id_Hang");
            txtTenHang.DataBindings.Clear();
            txtTenHang.DataBindings.Add("Text", dataGridView1.DataSource, "TenHang");
            txtDonGia.DataBindings.Clear();
            txtDonGia.DataBindings.Add("Text", dataGridView1.DataSource, "DonGia");
            dateTimePicker1.DataBindings.Clear();
            dateTimePicker1.DataBindings.Add("Text", dataGridView1.DataSource, "NSX");
            cbxNCC.DataBindings.Clear();
            cbxNCC.DataBindings.Add("Text", dataGridView1.DataSource, "TenNhaCungCap");
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        idHa = dataGridView1.Rows[id].Cells[0].Value.ToString();


                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if ((txtTenHang.Text == "") || (txtDonGia.Text == "") || (cbxNCC.Text == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin cần thiết!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (numBUO.isNumberInput(txtDonGia.Text) == false)
                {
                    MessageBox.Show("Trường đơn giá phải nhập số", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    tbl_Hang ha = new tbl_Hang();
                    ha.TenHang = txtTenHang.Text;
                    ha.NSX = DateTime.Parse(dateTimePicker1.Text);
                    ha.DonGia = int.Parse(txtDonGia.Text);
                    if (haBUO.Update(lblIDHang.Text, ha) == true)
                    {
                        dataGridView1.DataSource = haBUO.HienThiHangHoa();
                        dataGridView1.Enabled = true;
                        MessageBox.Show("Sửa hàng hóa thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        tbl_NhaCungCap ncc = (tbl_NhaCungCap)cbxNCC.SelectedItem;
                        tbl_NCC_Hang ncch = new tbl_NCC_Hang();
                        ncch.id_Hang = lblIDHang.Text;
                        ncch.id_NhaCungCap = haBUO.getIDNCC_Hang(ncc.TenNhaCungCap);
                        if (haBUO.UpdateNCC_Hang(lblIDHang.Text, ncch) == true)
                        {
                            dataGridView1.DataSource = haBUO.HienThiHangHoa();
                            //String aa = haBUO.getIDNCC_Hang(ncc.TenNhaCungCap);
                            //String bb = ncch.id_Hang;
                            //MessageBox.Show("cap nhat thanh cong: "+ bb + " : " + aa);
                        }
                        else
                        {
                            MessageBox.Show("ko thanh cong");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Sửa hàng hóa không thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }

            binding();
        }

        private void xóaHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu hàng hóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (haBUO.Delete(idHa))
                {
                    MessageBox.Show("Xóa hàng hóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa hàng hóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = haBUO.GetAll();
            }
        }
    }
}
